//
//  ViewController.m
//  UI_通讯录.通讯录.
//
//  Created by 男哥呵呵 on 16/1/21.
//  Copyright © 2016年 Mr.x. All rights reserved.
//

#import "ViewController.h"
#import "AdressBookModel.h"

@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImageView *headView;
@property (nonatomic, strong) UITextField *name;
@property (nonatomic, strong) UITextField *gender;
@property (nonatomic, strong) UITextField *phoneNum;
@property (nonatomic, strong) UITextField *adress;
@property (nonatomic, strong) AdressBookModel *model;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"添加联系人";
    self.navigationItem.hidesBackButton = YES;
    self.navigationController.navigationBar.translucent = NO;
    
    self.headView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width / 2 - 20, self.view.frame.size.height / 4 - 20)];
    self.headView.backgroundColor = [UIColor whiteColor];
    self.headView.userInteractionEnabled = YES;
    [self.view addSubview:self.headView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = self.headView.frame;
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.name = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 40, 50, self.view.frame.size.width / 2 - 80, 20)];
    self.name.backgroundColor = [UIColor whiteColor];
    self.name.placeholder = @"姓名";
    self.name.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.name];
    
    self.gender = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 40, 120, self.view.frame.size.width / 2 - 80, 20)];
    self.gender.backgroundColor = [UIColor whiteColor];
    self.gender.placeholder = @"性别";
    self.gender.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.gender];
    
    self.phoneNum = [[UITextField alloc] initWithFrame:CGRectMake(self.headView.frame.origin.x, 290, 360, 30)];
    self.phoneNum.backgroundColor = [UIColor whiteColor];
    self.phoneNum.placeholder = @"电话";
    self.phoneNum.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.phoneNum];
    
    self.adress = [[UITextField alloc] initWithFrame:CGRectMake(self.headView.frame.origin.x, 350, 360, 30)];
    self.adress.backgroundColor = [UIColor whiteColor];
    self.adress.placeholder = @"地址";
    self.adress.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.adress];
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeSystem];
    addButton.frame = CGRectMake(0, 450, self.view.frame.size.width / 2, 30);
    [addButton setTitle:@"添加" forState:UIControlStateNormal];
    addButton.backgroundColor = [UIColor whiteColor];
    [addButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addAction:)   forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addButton];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(self.view.frame.size.width / 2, 450, self.view.frame.size.width / 2, 30);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor whiteColor];
    [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelAction:)   forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelButton];
}

#pragma mark - 添加

- (void)addAction:(UIButton *)sender {
    self.model = [[AdressBookModel alloc] init];
    self.model.name = self.name.text;
    self.model.gender = self.gender.text;
    self.model.phoneNum = self.phoneNum.text;
    self.model.adress = self.adress.text;
    self.model.image = self.headView.image;
    
    [self.delegate sendModel:self.model];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

- (void)cancelAction:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 头像
- (void)buttonAction:(UIButton *)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.view.backgroundColor = [UIColor orangeColor];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;
    
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    self.headView.image = image;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
